package jp.ne.hatena.d.digitalsoul.warikan.pages

import java.text._
import java.util._

import org.apache.wicket.markup.html._
import org.apache.wicket.markup.html.basic._
import org.apache.wicket.markup.html.form._
import org.apache.wicket.markup.html.list._
import org.apache.wicket.model._

import jp.ne.hatena.d.digitalsoul.warikan.actions._
import jp.ne.hatena.d.digitalsoul.warikan.pages.forms._
import jp.ne.hatena.d.digitalsoul.warikan.pages.items._

class AccountingPage(val partyId:Int) extends WebPage {
	
	val action:AccountingAction = new AccountingAction
	
	val mainFormModel = action.createMainFormModel(partyId)
	
	val slopeItems = action.createSlopeItems
	
	var paymentListItems = new ArrayList[PaymentListItem]()
	
	// form:main
	val main = new Form("main", new CompoundPropertyModel(mainFormModel))
	add(main)
	
	// label:partyName
	main.add(new Label("partyName"))
	
	// label:schedule
	main.add(new Label("schedule"))
	
	// label:location
	main.add(new Label("location"))
	
	// label:sum
	main.add(new Label("sum"))
	
	// select:slopeId
	val slopeSelect = new DropDownChoice("selectedSlope", slopeItems)
	main.add(slopeSelect)
	
	// button:calculate
	val calculateButton = new Button("calculateButton") {
		override def onSubmit = {
			paymentListItems.clear
			val slopeId = mainFormModel.selectedSlope.id 
			action.calculate(partyId, slopeId, paymentListItems)
		}
	}
	main.add(calculateButton)
	
	// table:payments
	val paymentList = new ListView[PaymentListItem]("payments", paymentListItems) {
		override def populateItem(listItem:ListItem[PaymentListItem]) {

			// model
			val modelObject = listItem.getModelObject
			listItem.setModel(new CompoundPropertyModel(modelObject))
			
			// label
			listItem.add(new Label("name"))
			listItem.add(new Label("role"))
			listItem.add(new Label("payment"))
		}
	}
	main.add(paymentList)
	
	// button:back
	val backButton = new Button("backButton") {
		override def onSubmit = {
			setResponsePage(new TopPage)
		}
	}
	main.add(backButton)
	
}